<?php

namespace App\Service\Payment;

use App\Service\ServiceLog;

class AlipayService
{
    public static function unifiedOrder($orderArr = array())
    {
        $alipayConfig = config('payment.alipay');

        if (!isset($orderArr['order_id'])) {
            return false;
        }

        $api_params = array(
            'product_code' => 'QUICK_MSECURITY_PAY',
            'body' => $alipayConfig['body'],
            'subject' => $alipayConfig['subject'],
            'total_amount' => $orderArr['price'],
            'out_trade_no' => $orderArr['order_id'],
        );

        $api_params = json_encode($api_params,JSON_UNESCAPED_UNICODE);

        $time = date("Y-m-d H:i:s",time());
        $order = array(
            'biz_content' => $api_params,
            'app_id' => $alipayConfig['app_id'],
            'method' => 'alipay.trade.app.pay',
            'format' => 'json',
            'charset' => $alipayConfig['charset'],
            'notify_url' => $alipayConfig['notify_url'],
            'sign_type' => $alipayConfig['sign_type'],
            'timestamp' => $time,
            'version' => '1.0',
        );

        ksort($order);
        $orderInfo = self::createLinkstring($order);
        $sign = self::rsaSign($orderInfo,$alipayConfig['private_key']);
        $order['sign'] = $sign;


        return http_build_query($order);
    }

    /**
     * 对签名字符串转义
     * @param $para
     * @return string
     */
    public static function createLinkstring($para)
    {
        ksort($para);
        $arg = "";
        while (list ($key, $val) = each($para)) {
            $arg .= $key . '=' . $val . '&';
        }
        $arg = substr($arg, 0, count($arg) - 2);
        if (get_magic_quotes_gpc()) {
            $arg = stripslashes($arg);
        }

        return $arg;
    }

    /**
     *  签名生成订单信息
     */
    public static function rsaSign($data,$private_key)
    {
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($private_key, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";

        openssl_sign($data, $sign, $res,OPENSSL_ALGO_SHA256);
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * RSA验签
     * @param $data 待签名数据
     * @param $public_key 支付宝的公钥文件路径
     * @param $sign 要校对的的签名结果
     * @return 验证结果
     */
    public static function rsaVerify($data, $sign)
    {
        $alipayConfig = config('payment.alipay');
        $public_key = $alipayConfig['ali_public_key'];
        $public_key = chunk_split($public_key, 64, "\n");
        $public_key = "-----BEGIN PUBLIC KEY-----\n$public_key-----END PUBLIC KEY-----\n";
        $res = openssl_get_publickey($public_key);
        $result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_SHA256);
        openssl_free_key($res);
        return $result;
    }

    public static function getSignVerify($data, $sign)
    {
        //var_dump($data);
        //除去待签名参数数组中的空值和签名参数
        $params = array();
        while (list ($key, $val) = each($data)) {
            if ($key == "sign" || $key == "sign_type" || $val == "" || $key == "source") continue;
            else    $params[$key] = $data[$key];
        }
        ksort($params);
        reset($params);
        //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串

        $prestr = self::createLinkString($params);

        return self::rsaVerify($prestr, $sign);
    }

    /**
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     **/
    protected static function checkEmpty($value) {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }
}
        
        
        
        
        
        
        
        
        
        
        
        
        
        